/*
 *     (C) Copyright 2019, FlySky.
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 */

package sdk.webview.fmc.com.fmcsdk.util;

import android.content.Context;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;

import java.io.File;

import sdk.webview.fmc.com.fmcsdk.service.NetworkListenerService;


public class GlideImageLoader {
    /**
     * @param context context
     * @param url image url
     * @param iv imageView
     */

    private static final float IMG_THUMBNAIL = 1f;//    加载图片的缩略比例

    public static void loadImg(Context context, String url, ImageView iv) {
        if (!TextUtils.isEmpty(url)) {
            if (!url.contains("http")) {
                if (url.startsWith("/api")) {
                    String baseUrl = PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.BASE_ADDRESS_URL, "");
                    Glide.with(context)
                            .load(baseUrl + url)
                            .transition(DrawableTransitionOptions.withCrossFade())
                            .thumbnail(IMG_THUMBNAIL)
                            .into(iv);
                } else {
                    File file = new File(url);
                    Glide.with(context)
                            .load(file)
                            .transition(DrawableTransitionOptions.withCrossFade())
                            .thumbnail(IMG_THUMBNAIL)
                            .into(iv);
                }

            } else {

                    Glide.with(context)
                            .load(url)
                            .transition(DrawableTransitionOptions.withCrossFade())
                            .thumbnail(IMG_THUMBNAIL)
                            .into(iv);
            }

        }
    }


}
